import 'package:flutter/material.dart';
import 'package:flutter_yn_goods/base/controller/base_list_more_controller.dart';
import 'package:flutter_yn_goods/config/app_translations.dart';
import 'package:flutter_yn_goods/pages/mine_collect/model/model_collect_list.dart';
import 'package:flutter_yn_goods/user/app_user_manager.dart';
import 'package:flutter_yn_goods/utils/http/http_export.dart';
import 'package:flutter_yn_goods/utils/loading/loading_utils.dart';
import 'package:get/get.dart';

class MineCollectLogic extends BaseListMoreController {
  // tag
  static const String tag = 'MineCollectLogic';

  // 是否处于管理模式
  var isManageMode = false.obs;

  // 是否全选
  var isAllSelected = false.obs;

  // 选中的收藏商品数量
  var selectedCount = 0.obs;

  // 收藏商品总数
  var totalCount = 0.obs;

  @override
  void onInit() {
    super.onInit();

    HttpManager().get(
      'api/user/visit_list',
      params: {'type': 'user', 'uid': AppUserManager().uid},
      // fromJson: ModelCollectList.fromJson,
    );
    // pageSize = 10;
  }

  // 切换管理模式
  void toggleManageMode() {
    isManageMode.value = !isManageMode.value;
    if (!isManageMode.value) {
      // 退出管理模式时，取消所有选择
      _resetSelections();
    }
  }

  // 切换选择状态
  void toggleItemSelection(int index) {
    if (index >= 0 && index < list.length) {
      var item = list[index] as ModelCollectListList;
      item.isSelected = !item.isSelected;
      _updateSelectionStatus();
      update();
    }
  }

  // 切换全选状态
  void toggleSelectAll() {
    isAllSelected.value = !isAllSelected.value;
    for (var item in list) {
      (item as ModelCollectListList).isSelected = isAllSelected.value;
    }
    _updateSelectionStatus();
    update();
  }

  // 更新选择状态
  void _updateSelectionStatus() {
    int count = 0;
    bool allSelected = list.isNotEmpty;

    for (var item in list) {
      var collectItem = item as ModelCollectListList;
      if (collectItem.isSelected) {
        count++;
      } else {
        allSelected = false;
      }
    }

    selectedCount.value = count;
    isAllSelected.value = allSelected;
  }

  // 重置选择状态
  void _resetSelections() {
    for (var item in list) {
      (item as ModelCollectListList).isSelected = false;
    }
    selectedCount.value = 0;
    isAllSelected.value = false;
  }

  // 获取选中的商品ID列表
  List<dynamic> getSelectedIds() {
    List<dynamic> ids = [];
    for (var item in list) {
      var collectItem = item as ModelCollectListList;
      if (collectItem.isSelected) {
        ids.add(collectItem.productId);
      }
    }
    return ids;
  }

  // 批量取消收藏
  Future<bool> batchCancelCollect() async {
    List<dynamic> selectedIds = getSelectedIds();
    if (selectedIds.isEmpty) {
      LoadingUtils.showToast(LocaleKeys.please_select_items.tr);
      return false;
    }

    Map<String, dynamic> params = {
      'id': selectedIds,
      'type': AppUserManager().isLeader() ? 'leader' : 'user',
      'uid': AppUserManager().uid,
    };

    HttpResponse response =
        await HttpManager().post('api/collect/del', params: params);
    if (response.isSuccess) {
      // 删除成功后，从列表中移除选中的项
      list.removeWhere((item) => (item as ModelCollectListList).isSelected);
      _resetSelections();
      update();
      return true;
    }
    return false;
  }

  @override
  Future<List> loadData() async {
    Map<String, dynamic> params = {
      'page': pageNum,
      'limit': pageSize,
      'uid': AppUserManager().uid,
      'type': AppUserManager().isLeader() ? 'leader' : 'user',
      'relation_type': 'collect'
    };

    HttpResponse response = await HttpManager().get(
      'api/collect/user',
      params: params,
      fromJson: ModelCollectList.fromJson,
    );

    if (response.isSuccess) {
      // CollectListResponse collectResponse = response.data;
      // totalCount.value = collectResponse.count ?? 0;
      return response.data.list ?? [];
    }

    return [];
  }
}
